import os
import cgi

from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

import auth

# Datastore

import datastore



#
# Request handlers
#




class TreeCreateNewOnePage(webapp.RequestHandler):
    def get(self):
	# Authorize users from datastore list of allowed emails
        template_values = {}
        if auth.request_authorize(self, template_values, '/tree_create_new_one') == False:
            return

        # Generate html from django template.
        auth.request_generate(self,'templates/webtree_create_new.html',template_values)


class  TreeCreatePage(webapp.RequestHandler):
    def post(self):
	# Authorize users from datastore list of allowed emails
        template_values = {}
        if auth.request_authorize(self, template_values, '/a/tree_create') == False:
            return

        # get POST variables from cgi
        name_ = cgi.escape(self.request.get('name'))
        content_ = cgi.escape(self.request.get('content'))

        if name_ == '' or content_ == '':
            self.response.out.write('<h3>Error: empty form-fields</h3><p>Press the "back" button to correct and try again.</p>')
            return
#
####
########### TODO : insert public and anonymous into the new object please!!!!!
        anonymous_ = cgi.escape(self.request.get('anonymous'))
        public_ = cgi.escape(self.request.get('public'))

        a_ = False
        p_ = False
        if anonymous_ == 'anonymous':
            a_ = True
        if public_ == 'public':
            p_ = True


            
        # create and store it
        webtree = datastore.Webtree(name=name_, owner=users.get_current_user(), contents=content_, public=p_, anonymous=a_)
        webtree.put()

        self.redirect('/')








class TreeDeleteSomeOfMyOwnWebtreesPage(webapp.RequestHandler):
    def get(self):
	# Authorize users from datastore list of allowed emails
        template_values = {}
        if auth.request_authorize(self, template_values, '/tree_delete_some_of_my_own_webtrees') == False:
            return

        # Page specific code

        user = users.get_current_user() # this always returns something cuz we authorize only non-anonymous

        nickname = user.nickname()
        email = user.email()
        greeting = 'Hi there, ' + nickname + ':) '

        template_values['greeting'] = greeting

        # Generate html from django template.
        auth.request_generate(self,'templates/404.html',template_values)








class TreeForkAPublicOnePage(webapp.RequestHandler):
    def get(self):
	# Authorize users from datastore list of allowed emails
        template_values = {}
        if auth.request_authorize(self, template_values, '/tree_fork_a_public_one') == False:
            return

        # Page specific code

        user = users.get_current_user() # this always returns something cuz we authorize only non-anonymous

        nickname = user.nickname()
        email = user.email()
        greeting = 'Hi there, ' + nickname + ':) '

        template_values['greeting'] = greeting

        # Generate html from django template.
        auth.request_generate(self,'templates/404.html',template_values)





#
# Url switching table
#


application = webapp.WSGIApplication([
        ('/tree_delete_some_of_my_own_webtrees', TreeDeleteSomeOfMyOwnWebtreesPage),
        ('/tree_fork_a_public_one', TreeForkAPublicOnePage),
        ('/tree_create_new_one', TreeCreateNewOnePage),
        ('/a/tree_create', TreeCreatePage),
        ], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
